package xyh.ContributionSystem;

import ls.dao.StudentDAO;
import ls.entity.Student;
import ls.impl.StudentDAOImpl;
import zry.User.Teacher.*;
import xyh.ContributionSystem.ContributionSearch.SearchMember;
import xyh.ContributionSystem.ProjectContribution.*;
import xyh.ContributionSystem.ProjectMember.*;
import xyh.ContributionSystem.Projects.*;

import java.util.*;

public class ConSysByUser {
    private ProjectDAO projectDAO;
    private ProjectMemberDAO projectMemberDAO;
    private ProjectContributionDAO projectContributionDAO;

    public ConSysByUser(){
        this.projectDAO = new ProjectDAOImpl();
        this.projectMemberDAO = new ProjectMemberDAOImpl();
        this.projectContributionDAO = new ProjectContributionDAOImpl();
    }

    ContributionSystem contributionSystem = new ContributionSystem();

    //录入导师的项目情况
    public boolean addProjectSys(int teacherId) {
        List<Project> allProjects = contributionSystem.getAllProjects();
        int maxId = 0;
        for (Project project : allProjects) {
            maxId = Math.max(maxId, project.getP_id());
        }
        // 使用Scanner读取用户输入
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入项目名称: ");
        String name = sc.nextLine();
        System.out.print("请输入项目类型: ");
        String type = sc.nextLine();
        System.out.print("请输入项目开发时间: ");
        String time = sc.nextLine();
        System.out.print("请输入项目描述: ");
        String task = sc.nextLine();
        System.out.print("请输入项目总经费: ");
        double outlay = sc.nextDouble();

        // 使用输入的信息新建一个Project对象
        Project project = new Project(maxId+1, name, teacherId, type, time, task, outlay);

        //调用addProject新增项目
        return projectDAO.addProject(project);
    }

    //查看所有项目信息
    public void printAllProjects() {
        List<Project> projects = contributionSystem.getAllProjects();
        System.out.println("-----------------");
        for (Project project : projects) {
            System.out.print("项目编号: " + project.getP_id()+" " );
            System.out.print("项目名称: " + project.getP_name()+" " );
            System.out.print("项目导师编号: " + project.getTeacher_id()+" ");
            System.out.print("项目类型: " + project.getP_type()+" ");
            System.out.print("项目开发时间: " + project.getP_time()+" ");
            System.out.print("项目描述: " + project.getP_task()+" ");
            System.out.println("项目总经费: " + project.getP_outlay()+" ");
            System.out.println("-----------------");
        }
    }

    //修改项目信息
    public void updateProject() {
        List<Project> projects = projectDAO.getAllProjects();
        for (Project project : projects) {
            System.out.println("-----------------");
            System.out.print("项目编号: " + project.getP_id());
            System.out.print("项目名称: " + project.getP_name());
            System.out.print("项目导师编号: " + project.getTeacher_id());
            System.out.print("项目类型: " + project.getP_type());
            System.out.print("参与项目时间: " + project.getP_time());
            System.out.print("项目描述: " + project.getP_task());
            System.out.print("项目经费: " + project.getP_outlay());
            System.out.println("-----------------");
        }
        System.out.println("请输入想要修改的项目编号：");
        Scanner sc = new Scanner(System.in);
        int p_id = sc.nextInt();
        System.out.println("请输入新的项目经费：");
        double new_outlay = sc.nextDouble();
        boolean flag = contributionSystem.updateProjectOutlayById(p_id, new_outlay);
        if(flag)System.out.print("修改成功！");
        else System.out.print("修改失败！");
    }

    //学生申报项目贡献信息
    //展示所有项目选择
    //更新贡献表
    public void submitContribution(int studentid) {
        int studentId = studentid;
        /*
        List<Project> projectIds = contributionSystem.getAllProjects();
        if (projectIds.isEmpty()) {
            System.out.println("没有该项目");
            return;
        }
        */
        // 去重后排序
        Set<Integer> set = new HashSet<>();

        // 根据项目id输出项目信息
        List<Project> projects = contributionSystem.getAllProjects();
        for (Project pro : projects) {
            set.add(pro.getP_id());
            System.out.println(pro.getP_id() + " " + pro.getP_name());
        }
        System.out.println("请选择参加的项目编号:");
        Scanner scanner = new Scanner(System.in);
        int projectId = scanner.nextInt();
        if(!set.contains(projectId)){
            System.out.println("请检查输入项目编号！");
            return;
        }
        scanner.nextLine();
        System.out.println("请输入项目贡献信息:");
        String task = scanner.nextLine();
        System.out.println("请输入项目参与时间:");
        String time = scanner.nextLine();
        System.out.println("请输入承担工作的折合经费:");
        double outlay = scanner.nextDouble();
        String type = contributionSystem.getProjectInfo(projectId).getP_type();
        //新增贡献表
        List<ProjectContribution> list = contributionSystem.getAllProjectContribution();
        int maxId = 0;
        for (ProjectContribution projectContribution : list) {
            maxId = Math.max(maxId, projectContribution.getContribution_id());
        }
        boolean flag = contributionSystem.addProjectContribution(maxId+1,studentId,projectId,time,type,task,outlay);
        if(flag) System.out.println("添加项目贡献成功！");
        else System.out.println("添加项目贡献失败！");
    }

    //老师获取自己学生所有贡献表 //todo
    public void showContributionByTeacherId(int teacherid) {
        TeacherDAO teacherDAO = new TeacherDAOImpl();
        List<String> studentIds = teacherDAO.findStudents(String.valueOf(teacherid));
        //List<String> studentIds = new ArrayList<>();
        //studentIds.add("1");
        //studentIds.add("3");

        ProjectContributionDAO projectContributionDAO = new ProjectContributionDAOImpl();
        for(String studentId:studentIds) {
            List<ProjectContribution> contributions = contributionSystem.findContributionByStudentId(Integer.parseInt(studentId));
            for(ProjectContribution contribution:contributions) {
                System.out.println("-----------------");
                System.out.print("贡献表编号: " + contribution.getContribution_id());
                System.out.print("研究生学号: " + contribution.getStudent_id());
                System.out.print("项目编号: " + contribution.getP_id());
                System.out.print("项目类型: " + contribution.getP_type());
                System.out.print("参与项目时间: " + contribution.getP_time());
                System.out.print("承担工作: " + contribution.getP_task());
                System.out.print("承担工作的折合经费: " + contribution.getContribution_outlay());
                System.out.println("-----------------");
            }
        }
    }

    //老师选择贡献表更新
    public void updateContributionByTeacherId(int teacherid) {
        showContributionByTeacherId(teacherid);
        System.out.println("请输入要修改的贡献表编号：");
        Scanner scanner = new Scanner(System.in);
        int contribution_id = scanner.nextInt();
        //获取贡献信息
        ProjectContribution contribution = projectContributionDAO.getProjectContribution(contribution_id);
        boolean exitCode = true;
        while (exitCode) {
            System.out.println("请选择要修改的内容：");
            System.out.println("1.项目类型 2.参与项目时间 3.承担工作 4.承担工作的折合经费 5.退出修改");
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("请输入新的项目类型:");
                    String p_type = scanner.nextLine();
                    contribution.setP_type(p_type);
                    break;
                case 2:
                    System.out.println("请输入新的参与项目时间:");
                    String p_time = scanner.nextLine();
                    contribution.setP_time(p_time);
                    break;
                case 3:
                    System.out.println("请输入新的承担工作:");
                    String p_task = scanner.nextLine();
                    contribution.setP_task(p_task);
                    break;
                case 4:
                    System.out.println("请输入新的承担工作的折合经费:");
                    double contribution_outlay = scanner.nextDouble();
                    contribution.setContribution_outlay(contribution_outlay);
                    break;
                case 5:
                    exitCode = false;
                    break;
                default:
                    System.out.println("选择错误，请检查输入！");
                    System.out.println();
                    break;
            }
        }
        projectContributionDAO.updateProjectContribution(contribution);
    }

    //老师更新项目成员表中状态，项目贡献数+1
    //bug
    public void approveStudent(int teacherid) {
        // 获取所有学生ID
        TeacherDAO teacherDAO = new TeacherDAOImpl();
        List<String> studentIds = teacherDAO.findStudents(String.valueOf(teacherid));
        // 遍历学生ID
        //StudentDAO studentDAO = DAOFactory.getInstance().getStudentDAO();
        //List<String> studentIds = new ArrayList<>();
        //studentIds.add("1");
        //studentIds.add("3");

        StudentDAO studentDAO = new StudentDAOImpl();
        for (String studentId : studentIds) {
            //通过student_id查询学生信息
            Student student = studentDAO.getStudent(studentId);
            System.out.println("学生学号: " + student.getStudent_id() + ", 学生姓名: " + student.getStudent_name());
        }

        //询问用户选择学生
        System.out.println("请输入要审核的学生学号:");
        Scanner scanner = new Scanner(System.in);
        //todo String studentId = scanner.nextLine();Integer.parseInt(
        int studentId = scanner.nextInt();
        SearchMember searchMember = new SearchMember();
        searchMember.setStudent_id(studentId);
        // 查找该学生的项目信息
        List<ProjectMember> projectMembers = projectMemberDAO.findProjectMember(searchMember);

        List<Integer> projectIds = new ArrayList<Integer>();
        for(ProjectMember projectMem : projectMembers){
            //将项目ID存在集合内然后输出每个项目信息
            int projectId = projectMem.getP_id();
            projectIds.add(projectId);
        }
        System.out.println("-----------------");
        for(int i=0;i<projectIds.size();i++){
            //根据id查询项目信息
            Project project = contributionSystem.getProjectInfo(projectIds.get(i));
            System.out.print("项目编号: " + project.getP_id()+" " );
            System.out.print("项目名称: " + project.getP_name()+" " );
            System.out.print("项目导师编号: " + project.getTeacher_id()+" ");
            System.out.print("项目类型: " + project.getP_type()+" ");
            System.out.print("项目开发时间: " + project.getP_time()+" ");
            System.out.print("项目描述: " + project.getP_task()+" ");
            System.out.println("项目总经费: " + project.getP_outlay()+" ");
            System.out.println("-----------------");
        }
        System.out.println("请选择该学生参与的项目ID");
        int proId = scanner.nextInt();
        //根据项目查找出贡献表
        List<ProjectContribution> projectContributions = contributionSystem.findContributionByStudentIdAndProjectId(studentId,proId);
        double sum = 0;
        for (ProjectContribution proCon : projectContributions) {
            sum += proCon.getContribution_outlay();
        }
        if(sum > 6){
            //将这名学生置为true
            contributionSystem.updateProjectMemberStatus(proId,studentId,1);
            System.out.println("项目金额达到标准！审核通过！");
        }
    }

}
